//reduce()-->从数组的第一项开始，逐个遍历到最后；
function count(arr, item) {
    //返回数组中指定元素的个数
        // prev是每次返回的值
         var count = arr.reduce(function(prev, curr) {
         // 三目运算,判断当前的值和指定的值是否相等来返回值
              return curr === item ? prev+1 : prev;
          }, 0);
          return count;
      }

function online_data_base() {
    var is_start_state=false;   //是否开启监控在线模式数据的变化
    var open_pin_map=new Object();   //开启mrc板子的管脚  key是block id  value 管脚编号
    var pin_value_map=new Object();  //mrc板子的管脚和值的的映射  key是管脚编号 value是管脚的值

    this.check_start_state=function () {
        /*检测是否开启在线监控*/
        return is_start_state;
    };

    this.set_start_state=function (state) {
        /*设置是否开启在线监控*/
        if(state==true || state==false){
            is_start_state=state;
        }
    };

    this.set_pin_mode=function (block_id,pin) {
        /*定义管脚的是否启动监测*/
        if(is_start_state==true){
            open_pin_map[block_id]=pin;
            if(pin=="M1"){
                var select='[pin_number="3"]';
                $(select).css("display","block");

                var select='[pin_number="5"]';
                $(select).css("display","block");
            }else if(pin=="M2"){
                var select='[pin_number="6"]';
                $(select).css("display","block");

                var select='[pin_number="9"]';
                $(select).css("display","block");
            }else if(pin=="MT"){
                var select='[pin_number="3"]';
                $(select).css("display","block");

                var select='[pin_number="5"]';
                $(select).css("display","block");

                var select='[pin_number="6"]';
                $(select).css("display","block");

                var select='[pin_number="9"]';
                $(select).css("display","block");

            }else{
                var select='[pin_number="'+pin+'"]';
                $(select).css("display","block");
            }


        }

    };
    this.get_pin_mode_map=function () {
      return open_pin_map
    };

    this.remove_pin_mode=function (block_id) {
        /*移除管脚的定义，关闭指定管脚的监控*/
        if(is_start_state==true){

            if(open_pin_map[block_id]=="M1"){
                var select='[pin_number="3"]';
                $(select).css("display","none");
                $(select).text("0");

                var select='[pin_number="5"]';
                $(select).css("display","none");
                $(select).text("0");
            }else if(open_pin_map[block_id]=="M2"){
                var select='[pin_number="6"]';
                $(select).css("display","none");
                $(select).text("0");

                var select='[pin_number="9"]';
                $(select).css("display","none");
                $(select).text("0");
            }else if(open_pin_map[block_id]=="MT"){
                var select='[pin_number="3"]';
                $(select).css("display","none");
                $(select).text("0");

                var select='[pin_number="5"]';
                $(select).css("display","none");
                $(select).text("0");

                var select='[pin_number="6"]';
                $(select).css("display","none");
                $(select).text("0");

                var select='[pin_number="9"]';
                $(select).css("display","none");
                $(select).text("0");
            }else{
                var pin=open_pin_map[block_id];
                if (count(Object.values(open_pin_map),pin)<2){
                   var select='[pin_number="'+open_pin_map[block_id]+'"]';
                  $(select).css("display","none");
                  $(select).text("0");
                }
            }
            delete open_pin_map[block_id]
        }
    };

    this.set_pin_value=function (pin,value) {
        /*设置管脚的值*/
        if(is_start_state==true){
            pin_value_map[pin]=value;
            var select='[pin_number="'+pin+'"]';
            $(select).text(value);

        }
    };

    this.get_pin_value=function (pin) {
        /*获取管脚的值*/
        if(is_start_state==false){
            return 0
        }
        else{
            if(pin_value_map[pin]==undefined){
                return 0
            }else{
                return pin_value_map[pin]
            }
        }
    };

    this.clear_pin_value=function () {
        /*把管脚的值全部置0*/
        for (let key in pin_value_map){
            pin_value_map[key]=0;
            var select='[pin_number="'+key+'"]';
            $(select).text("0");
        }
    }
}

// var online_data_obj=online_data();
